package com.dk.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
//        配置过滤器
        Map<String,String> filterMap=new LinkedHashMap<>();
//        filterMap.put("/admin/login","anon");
//        filterMap.put("/admin/register","anon");
//        filterMap.put("/admin/code","anon");
//        factoryBean.setLoginUrl("/user/toLogin");
//        filterMap.put("/js/**","anon");
//        filterMap.put("/css/**","anon");
//        filterMap.put("/image/**","anon");
//        filterMap.put("/static/**","anon");
//        filterMap.put("/logout","logout");
//authc必须认证才能登录
//annon无需登录即可访问
//        filterMap.put("/orderController/toGetOrderInfo","authc");
//        filterMap.put("/orderController/toHomeIndex","authc");
//        filterMap.put("/**","anon");

        //filterMap.put("")
        factoryBean.setSecurityManager(defaultWebSecurityManager);
//        factoryBean.setLoginUrl("/userController/toLogin");
//        factoryBean.setUnauthorizedUrl("/userController/noAuth");
        factoryBean.setFilterChainDefinitionMap(filterMap);
        return factoryBean;
    }
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(@Autowired LoginRealm loginRealm){
//        安全管理器
        DefaultWebSecurityManager manager=new DefaultWebSecurityManager();
        manager.setRealm(loginRealm);
        return manager;
    }
    @Bean
    public LoginRealm loginRealm(@Autowired HashedCredentialsMatcher hashedCredentialsMatcher){
        LoginRealm realm=new LoginRealm();
        realm.setCredentialsMatcher(hashedCredentialsMatcher);
        return realm;
    }
    @Bean
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher matcher=new HashedCredentialsMatcher();
        matcher.setHashAlgorithmName("MD5");
        matcher.setHashIterations(3);
        matcher.setStoredCredentialsHexEncoded(true);
        return matcher;
    }
//    @Bean
//    public ShiroDialect shiroDialect(){
//        return new ShiroDialect();
//    }
}
